package com.hf.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.Transaction;

import tool.ClientTool;
import tool.JsonTool;
import tool.StringTool;




import com.base.bean.*;
import com.base.dao.*;
import com.base.domain.ClientDetail;
import com.base.service.*;
import com.hf.bean.SimpleAddress;

public class ClientAction extends BaseAction{	
	private ClientService cs=ClientService.getInstance(); // 私有无geter seter
	@Deprecated
	private String[] phones,arr_phones;		//	
	private Integer cid;		//客户id值，在修改的时候会传入
	private String name,comName,memo;
	private String phone1,phone2,phone3;	//看设置几个电话号码
	private String[] chose1,chose2,chose3; //多选
	private String chose4,chose5,chose6; //单选
	private String type;	//a:显示(默认)   b：新增
	
	private String add1Code,add2Code,add3Code;
	private List<SimpleAddress> add1,add2,add3;
	
	private AddressService as=AddressService.getInstance();
	
	public ClientAction() {
		// TODO Auto-generated constructor stub
		phone1="";
		phone2="";
		phone3="";
		name="";
		comName="";
		memo="";
		this.type="a";
		// 其他未定义的需要判定是否为null
		
		
	}



	//新增客户 
	public String addClient() throws IOException{
	System.out.println("type:"+type);
	add1=as.getAddress();					//获取省 市 区
	add2=as.getAddress(1,add1Code);
	add3=as.getAddress(2,add2Code);
	if(type=="a")	
		return SUCCESS;
	try{	
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();		
//		User user=(User)session.getAttribute("user");	//正式用替换下面这句  
		User user=new User(new Integer(1));
		
		
		phone1=(phone1!=null)&&(phone1.trim()!="")?phone1:"";
		phone2=(phone1!=null)&&(phone2.trim()!="")?phone2:"";
		phone3=(phone1!=null)&&(phone3.trim()!="")?phone3:"";
		
//		phones=new String[]{phone1,phone2,phone3};
		arr_phones=new String[]{phone1,phone2,phone3};
		
		chose4=(chose4!=null)&&(chose4.trim()!="")?chose4:"0";
		chose5=(chose5!=null)&&(chose5.trim()!="")?chose5:"0";
		chose6=(chose6!=null)&&(chose6.trim()!="")?chose6:"0";

		
		
		name=(name!=null)&&(name.trim()!="")?name:"";
		
		if(!check()){						//验证
			return SUCCESS;
		}
			
		ClientBase cBase=new ClientBase();
		cBase.setOwner(user.getUid());
		cBase.setName(name);
		cBase.setComName(comName);
		cBase.setProvinceId(add1Code);
		cBase.setCityId(add2Code);
		cBase.setAreaId(add3Code);
		
		
		
		ClientExtend cExtend=new ClientExtend();
		cExtend.setId(new ClientExtendId(new Integer(0),user.getUid()));
		cExtend.setMemo(memo.trim());
		cExtend.setYywjj(StringTool.arrToStr(chose1));
		cExtend.setYyzzj(StringTool.arrToStr(chose2));
		cExtend.setYgsbType(StringTool.arrToStr(chose3));
		cExtend.setClientType(chose4);
		cExtend.setIsZan(chose5);
		cExtend.setGjTimeType(chose6);
		
		this.setCid(cs.addClient(cBase, cExtend, StringTool.arrToStr(arr_phones))); //添加成功返回该cid值
		
		
		return SUCCESS;
	}catch(Exception e){
		e.printStackTrace();
		this.setTip("出现未知错误，若反复出现请联系管理员！");
		return ERROR;
	}
		
	}

	// 各种组合条件的查询实现 看能否和execute整合
	public String show(){
		
		return SUCCESS;
	}
	


	// 根据cid 显示客户所有信息
	public String showById(){
		this.showClient(this.cid);
		return SUCCESS;
	}
	
	
	//默认显示客户  按时间顺序  
	@Override
	public String execute() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	// 显示客户详细信息  注意需要传入cid参数
	private void showClient(Integer cid){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();		
//		User user=(User)session.getAttribute("user");	//正式用替换下面这句  
		User user=new User(new Integer(1));
		
		ClientBase cb=cs.getClientBaseDao().findByHql("from ClientBase a where c.cid="+cid).get(0);
		ClientExtend ce=cs.getClientExtendDao().findByHql("from ClientExtend b where b.id.cid=" 
				+ cid +  " and b.id.creator="+user.getUid()).get(0);
		
		List<ClientPhone> cps=cs.getClientPhoneDao().findByHql("from ClientPhone c where c.cid="+cid);
		for(int i=0;i<cps.size();i++){			// 电话只取3条
			if(i==0) phone1=cps.get(0).getPhone();
			if(i==1) phone2=cps.get(1).getPhone();
			if(i==2) phone3=cps.get(2).getPhone();
			if(i>2) break;
		}
		
		this.name=cb.getName();
		this.comName=cb.getComName();
		this.memo=ce.getMemo();
		chose1=ce.getYywjj().split(",");
		chose2=ce.getYyzzj().split(",");
		chose3=ce.getYgsbType().split(",");
		
		chose4=ce.getClientType();
		chose5=ce.getIsZan();
//		chose6=ce.getGjTimeType();		//这个应该是当次有效
		add1Code=cb.getProvinceId();
		add2Code=cb.getCityId();
		add3Code=cb.getAreaId();
		
		add1=as.getAddress();
		add2=as.getAddress(1,add1Code);
		add3=as.getAddress(2,add2Code);
		
		// 当前值整到最前面
		for(SimpleAddress ad1:add1){
			if(ad1.getCode().equals(add1Code))
				add1.add(0, ad1);
		}
		
		for(SimpleAddress ad2:add2){
			if(ad2.getCode().equals(add2Code))
				add2.add(0, ad2);
		}
		
		for(SimpleAddress ad3:add3){
			if(ad3.getCode().equals(add3Code))
				add3.add(0, ad3);
		}
		
		
	}
	
	//检查名字和电话
	private boolean check(){
	boolean result=false;
	this.tip="";
	try{
		if(this.name!=null){
			if(ClientTool.isName(this.name)!=0){
				this.tip=" 名字不通过 "; 
				return result;
			}
		}
		if(this.phone1!=null || this.phone1!=""){
			int i=cs.checkPhone(phone1);
			if(i==1){
				this.tip=tip+" 电话1格式错误 ";
				return result;
			}
			if(i==2){
				this.tip=tip+" 电话1重复 ";
				return result;
			}
		}
		result=true;
		return result;
		
	}catch(Exception e){
		this.tip="验证信息时发生其他异常格式错误，请联系管理员！";
		return result;
	}
	
	}
	
	
	// geter and seter


	public String[] getPhones() {
		return phones;
	}


	public void setPhones(String[] phones) {
		this.phones = phones;
	}





	public Integer getCid() {
		return cid;
	}


	public void setCid(Integer cid) {
		this.cid = cid;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getComName() {
		return comName;
	}


	public void setComName(String comName) {
		this.comName = comName;
	}




	public String getAdd1Code() {
		return add1Code;
	}


	public void setAdd1Code(String add1Code) {
		this.add1Code = add1Code;
	}


	public String getAdd2Code() {
		return add2Code;
	}


	public void setAdd2Code(String add2Code) {
		this.add2Code = add2Code;
	}


	public String getAdd3Code() {
		return add3Code;
	}


	public void setAdd3Code(String add3Code) {
		this.add3Code = add3Code;
	}


	public String getMemo() {
		return memo;
	}


	public void setMemo(String memo) {
		this.memo = memo;
	}


	public String getPhone1() {
		return phone1;
	}


	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}


	public String getPhone2() {
		return phone2;
	}


	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}


	public String getPhone3() {
		return phone3;
	}


	public void setPhone3(String phone3) {
		this.phone3 = phone3;
	}


	public String[] getChose1() {
		return chose1;
	}


	public void setChose1(String[] chose1) {
		this.chose1 = chose1;
	}


	public String[] getChose2() {
		return chose2;
	}


	public void setChose2(String[] chose2) {
		this.chose2 = chose2;
	}


	public String[] getChose3() {
		return chose3;
	}


	public void setChose3(String[] chose3) {
		this.chose3 = chose3;
	}


	public String getChose4() {
		return chose4;
	}


	public void setChose4(String chose4) {
		this.chose4 = chose4;
	}


	public String getChose5() {
		return chose5;
	}


	public void setChose5(String chose5) {
		this.chose5 = chose5;
	}


	public String getChose6() {
		return chose6;
	}


	public void setChose6(String chose6) {
		this.chose6 = chose6;
	}





	public List<SimpleAddress> getAdd1() {
		return add1;
	}





	public void setAdd1(List<SimpleAddress> add1) {
		this.add1 = add1;
	}





	public List<SimpleAddress> getAdd2() {
		return add2;
	}


	public void setAdd2(List<SimpleAddress> add2) {
		this.add2 = add2;
	}



	public List<SimpleAddress> getAdd3() {
		return add3;
	}


	public void setAdd3(List<SimpleAddress> add3) {
		this.add3 = add3;
	}



	public String getType() {
		return type;
	}



	public void setType(String type) {
		this.type = type;
	}


	
	

	








	











}
